package com.osi.mes.qm.camera;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * Custom JButton
 * 
 */
public class JRButton extends JButton {
	private static final long serialVersionUID = 39082560987930759L;

	/** 默认背景色 */
	private Color backColor = new Color(125, 161, 237);

	/** 默认前景色 */
	private Color foregColor = new Color(91, 118, 173);

	/** 鼠标覆盖时的前景色 */
	private Color foregColorOnMouseEnter = Color.WHITE;

	private Color c1 = new Color(0, 0, 0);
	private Color c2 = new Color(100, 100, 100);
	private Color c3 = new Color(0, 0, 0, 50);
	private Color c4 = new Color(255, 255, 255, 100);

	private boolean hover;

	 public JRButton(ImageIcon icon){
	 this("");
	 this.setIcon(icon);
	 }

	public JRButton() {
		this("");
	}

	public JRButton(String text) {
		setFont(new Font("system", Font.PLAIN, 12));
		setBorderPainted(false);
		if (isEnabled()) {
			setForeground(foregColor);
		} else {
			setForeground(Color.LIGHT_GRAY);
		}
		setFocusPainted(false);
		setContentAreaFilled(false);
		setFont(new Font("宋体", Font.PLAIN, 12));
		setText(text);
		addListener();
	}

	public void setEnabled(boolean isEnabled) {
		if (isEnabled) {
			setForeground(foregColor);
		} else {
			setForeground(Color.LIGHT_GRAY);
		}
		super.setEnabled(isEnabled);
	}

	private void addListener() {
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				if (!isEnabled()) {
					setForeground(Color.LIGHT_GRAY);
					hover = false;
					return;
				}
				setForeground(foregColorOnMouseEnter);
				hover = true;
			}

			public void mouseExited(MouseEvent e) {
				if (!isEnabled()) {
					setForeground(Color.LIGHT_GRAY);
					hover = false;
					return;
				}
				setForeground(foregColor);
				hover = false;
			}
		});
	}

	public void setBackColor(Color back) {
		this.backColor = back;
		// this.foregColor = foreg;
	}
	
	public void setForeColor(Color foreg) {
		this.foregColor = foreg;
	}

	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		int h = getHeight();
		int w = getWidth();
		float tran = 1F;
		if (!hover) {
			tran = 0.3F;
		}

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		GradientPaint p1;
		GradientPaint p2;

		if (getModel().isPressed()) {
			p1 = new GradientPaint(0, 0, c1, 0, h - 1, c2);
			p2 = new GradientPaint(0, 1, c3, 0, h - 3, c4);
		} else {
			p1 = new GradientPaint(0, 0, c2, 0, h - 1, c1);
			p2 = new GradientPaint(0, 1, c4, 0, h - 3, c3);
		}
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
				tran));
		RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1,
				h - 1, 20, 20);

		Shape clip = g2d.getClip();
		g2d.clip(r2d);
		GradientPaint gp = new GradientPaint(0.0F, 0.0F, backColor, 0.0F, h,
				foregColor, true);
		g2d.setPaint(gp);
		g2d.fillRect(0, 0, w, h);
		g2d.setClip(clip);
		g2d.setPaint(p1);
		g2d.drawRoundRect(0, 0, w - 1, h - 1, 20, 20);
		g2d.setPaint(p2);
		g2d.drawRoundRect(1, 1, w - 3, h - 3, 18, 18);
		g2d.dispose();
		super.paintComponent(g);
	}

}
